home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / scherz programme / fortune / source / makefort.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  7KB  |  343 lines

  1. /*  Source Control
  2. $RCS
  3.  
  4. $version 0.2
  5.  
  6. $date Wed Jun 17 14:19:36 1992
  7.  
  8. $changes :
  9.  
  10. Revision 0.2    Jim Wed Jun 17 14:19:36 1992
  11. Now adds fortunes to the END of the linked list!
  12.  
  13. Revision 0.1    Jim Fri May 31 11:40:06 1991
  14. Now makes icons
  15.  
  16. Revision 0.0    Jim Mon Sep 10 19:22:19 1990
  17. Added to RCS
  18.  
  19. */
  20.  
  21. #include <exec/types.h>
  22. #include <exec/tasks.h>
  23. #include <libraries/dosextens.h>
  24. #include <workbench/workbench.h>
  25. #include <proto/all.h>
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30.  
  31. #define MAXSIZE 5000
  32. char *Version="Makefort Version 0.3, written by Jim Finnis.\n";
  33.  
  34. extern int writeshort(BPTR,int);
  35. extern int writelong(BPTR,long);
  36. extern int writestring(BPTR,char *);
  37. extern void savehuffman(BPTR);
  38. extern void genhuffman(void);
  39. extern void freehuffman(void);
  40. extern void clearhuffman(void);
  41. extern void addtohuffman(char *);
  42. extern char *compress(char *);
  43.  
  44. char *infile="fortunes.dat";
  45. char *outfile="fortunes";
  46.  
  47. /* data from file dataicon.brush.me2 */
  48. USHORT IconImageData[]={
  49. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,
  50. 0x0,    0xfff,    0x8000,    0x0,    0x1,    0xf000,    0x7c00,    0x0,
  51. 0xe,    0x0,    0x380,    0x0,    0x30,    0x1f0,    0x60,    0x0,
  52. 0xc0,    0x318,    0x18,    0x0,    0x300,    0x30,    0x6,    0x0,
  53. 0x400,    0x60,    0x1,    0x0,    0x800,    0x60,    0x0,    0x8000,
  54. 0x1000,    0x0,    0x0,    0x4000,    0x1000,    0x60,    0x0,    0x4000,
  55. 0x2000,    0x0,    0x0,    0x2000,    0x2000,    0x7010,    0x0,    0x2000,
  56. 0x2000,    0x4998,    0xc000,    0x2000,    0x2000,    0x4850,    0x2000,    0x2000,
  57. 0x2000,    0x4bd1,    0xe000,    0x2000,    0x1000,    0x71d0,    0xe000,    0x4000,
  58. 0x1000,    0x0,    0x0,    0x4000,    0x800,    0x60,    0x0,    0x8000,
  59. 0x400,    0x1fd0,    0x1e01,    0x0,    0x303,    0xf01d,    0xedf6,    0x0,
  60. 0xfe,    0xf,    0x18,    0x0,    0x30,    0x0,    0x60,    0x0,
  61. 0xe,    0x0,    0x380,    0x0,    0x1,    0xf000,    0x7c00,    0x0,
  62. 0x0,    0xfff,    0x8000,    0x0,    0x0,    0x0,    0x0,    0x0,
  63. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,
  64. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,
  65. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,
  66. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,
  67. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,
  68. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,
  69. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,
  70. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,
  71. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,
  72. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0,
  73. 0x0,    0x0,    0x0,    0x0,    0x0,    0x20,    0x0,    0x0,
  74. 0x0,    0xfe0,    0x1200,    0x0,    0x1,    0xfff0,    0xffe0,    0x0,
  75. 0xf,    0xffff,    0xff80,    0x0,    0x1,    0xffff,    0xfc00,    0x0,
  76. 0x0,    0xfff,    0x8000,    0x0,    0x0,    0x0,    0x0,    0x0,
  77. 0x0,    0x0,    0x0,    0x0,    0x0,    0x0,    0x0
  78. };
  79. /* end of data from file dataicon.brush.me2 */
  80.  
  81. struct Image IconImage=
  82. {0,0,53,29,0x0002,&IconImageData[0],0x0003,0,NULL};
  83.  
  84.  
  85. UBYTE *ttypes[]=
  86. {
  87.     "FILETYPE=Fortunes",
  88.     NULL
  89. };
  90.  
  91. char deftoolname[]="fortune";
  92.  
  93. struct DiskObject ProjIcon=
  94. {
  95.     WB_DISKMAGIC,WB_DISKVERSION,
  96.     {
  97.         NULL,0,0,53,29,GADGIMAGE|GADGHBOX,GADGIMMEDIATE|RELVERIFY,
  98.         BOOLGADGET,(APTR)&IconImage,NULL,NULL,NULL,NULL,0,NULL
  99.     },
  100.     WBPROJECT,
  101.     deftoolname,
  102.     ttypes,
  103.     NO_ICON_POSITION,
  104.     NO_ICON_POSITION,
  105.     NULL,NULL,6000
  106. };
  107.  
  108. struct FortList
  109. {
  110.     char *data;
  111.     unsigned long pos;
  112.     struct FortList *next;
  113. };
  114.  
  115. struct FortList *Head=NULL,*Tail=NULL;
  116. long number=0;
  117.  
  118. void ReadFortunes(fn)
  119. char *fn;
  120. {
  121.     FILE *a;
  122.     char c,oc,buf[MAXSIZE];
  123.     struct FortList *new;
  124.     int bad=0,i=0,n=0,len;
  125.  
  126.     printf(Version);
  127.     a=fopen(fn,"r");
  128.     if(a==NULL)
  129.     {
  130.         printf("File not found\n"); exit(200);
  131.     }
  132.  
  133.     clearhuffman();
  134.     printf("Reading...\n");
  135.     for(;;)
  136.     {
  137.         for(;;)
  138.         {
  139.             c=getc(a);
  140.             if(feof(a))break;
  141.             if(c=='%')
  142.             {
  143.                 oc=getc(a);
  144.                 if(oc!='%')
  145.                 {
  146.                     buf[i++]=c; buf[i++]=oc;
  147.                 }
  148.                 else
  149.                 {
  150.                     while(!feof(a)&&(getc(a)!='\n'));
  151.                     break;
  152.                 }
  153.             }
  154.             else
  155.             {
  156.                 if(c=='\t')
  157.                 {
  158.                     buf[i++]=' ';
  159.                     buf[i++]=' ';
  160.                     buf[i++]=' ';
  161.                     buf[i++]=' ';
  162.                     buf[i++]=' ';
  163.                 }
  164.                 else
  165.                 {
  166.                     buf[i++]=c;
  167.                 }
  168.             }
  169.  
  170.             if(i>=MAXSIZE-1)
  171.             {
  172.                 bad=1;
  173.                 break;
  174.             }
  175.  
  176.         }
  177.         if(bad)
  178.         {
  179.             printf("Fortune too big.\n");
  180.             fclose(a);
  181.             exit(0);
  182.         }
  183.         printf("%d Read %d chars.  \r",n++,i);
  184.         buf[i]=NULL; i=0;
  185.         addtohuffman(buf);
  186.  
  187.         new=(struct FortList *)malloc(sizeof(struct FortList));
  188.         if(new==NULL)
  189.         {
  190.             printf("Unable to allocate node\n");
  191.             exit(200);
  192.         }
  193.  
  194.         if(!(len=strlen(buf)))
  195.         {
  196.             printf("Zero length string found - please correct.\n");
  197.             exit(200);
  198.         }
  199.         new->data=(char *)malloc(len+1);
  200.         if(new->data==NULL)
  201.         {
  202.             printf("Unable to allocate string\n");
  203.             exit(200);
  204.         }
  205.         strcpy(new->data,buf);
  206.         new->next=NULL;
  207.         if(!Head)Head=new;
  208.         if(Tail)Tail->next=new;
  209.         Tail=new;
  210.         number++;
  211.  
  212.         if(feof(a))break;
  213.     }
  214. }
  215.  
  216. struct Library *IconBase=NULL;
  217.  
  218. BOOL MakeIcon(UBYTE *name,char **newtooltypes,char *newdeftool)
  219. {
  220.         struct DiskObject *dobj;
  221.         char *olddeftool;
  222.         char **oldtooltypes;
  223.         BOOL success=0;
  224.  
  225.         if(!(IconBase=OpenLibrary("icon.library",0L)))
  226.             return(FALSE);
  227.  
  228.         if(dobj=GetDiskObject(name))
  229.         {
  230.                 oldtooltypes=dobj->do_ToolTypes;
  231.                 olddeftool=dobj->do_DefaultTool;
  232.                 dobj->do_ToolTypes=newtooltypes;
  233.                 dobj->do_DefaultTool=newdeftool;
  234.                 success=PutDiskObject(name,dobj);
  235.                 dobj->do_ToolTypes=oldtooltypes;
  236.                 dobj->do_DefaultTool=olddeftool;
  237.                 FreeDiskObject(dobj);
  238.         }
  239.         if(!success)
  240.                 success=PutDiskObject(name,&ProjIcon);
  241.     if(IconBase)CloseLibrary(IconBase);
  242.         return(success);
  243. }
  244.  
  245. int CheckStack(void)
  246. {
  247.     LONG stacksize;
  248.     struct Process *myproc;
  249.  
  250.     if(!(myproc=(struct Process *)FindTask(NULL)))
  251.     {
  252.         printf("Oh dear, I don't know who I am...\n");
  253.         exit(4000);
  254.     }
  255.  
  256.     stacksize=((struct CommandLineInterface *)
  257.         ((myproc->pr_CLI)<<2))->cli_DefaultStack<<2;
  258.  
  259.     return(stacksize<30000L ? 1 : 0);
  260. }
  261.  
  262.  
  263. void main(int argc,char *argv[])
  264. {
  265.     BPTR out;
  266.     unsigned long pos=0;
  267.     char *inpf=infile,*outf=outfile;
  268.     struct FortList *p;
  269.     int n;
  270.  
  271.     if(CheckStack())
  272.     {
  273. printf("You have insufficient stack in this CLI. Correct this by using the\n");
  274. printf("command 'stack 40000' and then trying again.\n");
  275.         exit(100);
  276.     }
  277.  
  278.     if(argc>1)
  279.     {
  280.         inpf=argv[1];
  281.         if(argc>2)
  282.             outf=argv[2];
  283.     }
  284.  
  285.     /* First, read the fortunes and build the table */
  286.  
  287.     ReadFortunes(inpf);
  288. printf("all read!!\n");fflush(stdout);
  289.     genhuffman();
  290. printf("huffman genned!!\n");fflush(stdout);
  291.     freehuffman();
  292. printf("huffman freed!!\n");fflush(stdout);
  293.  
  294.     out=Open(outf,MODE_OLDFILE);
  295.     if(out==NULL)
  296.     {
  297.         printf("Unable to open file\n");
  298.         exit(200);
  299.     }
  300. printf("file opened!!\n");fflush(stdout);
  301.  
  302.     /* Write out the huffman table */
  303.  
  304.     savehuffman(out);
  305. printf("huffman saved!!\n");fflush(stdout);
  306.  
  307.     /* Write how many fortunes there are */
  308.  
  309.     writelong(out,number);
  310.  
  311.     /* Start position for fortunes */
  312.  
  313.     pos=4*(number+1)+20*16;
  314.  
  315.     /* Now go through the FortList, writing the jump table and setting
  316.     up the positions */
  317.  
  318.     printf("Writing jump table\n");
  319.     for(p=Head;p!=NULL;p=p->next)
  320.     {
  321.         writelong(out,pos);
  322.         p->pos=pos;
  323.         pos+=strlen(compress(p->data))+1;
  324.     }
  325. printf("JT writted!!\n");fflush(stdout);
  326.  
  327.     /* The fortunes are written out */
  328.  
  329.     for(n=0,p=Head;p!=NULL;p=p->next)
  330.     {
  331.         char *t;
  332.         printf("%d Writing...  \r",n++);
  333.         t=compress(p->data);
  334.         Write(out,t,strlen(t)+1);
  335.     }
  336.     Close(out);
  337.  
  338.     printf("%ld fortunes done.\n",number);
  339.     if(!(MakeIcon(outf,ttypes,deftoolname)))
  340.         printf("unable to write icon!\n");
  341. }
  342.  
  343.